package com.example.appointment.repository;

import com.example.appointment.entity.PostFavorite;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Repository
public interface PostFavoriteRepository extends JpaRepository<PostFavorite, Long> {
    Page<PostFavorite> findByUserId(Long userId, Pageable pageable);
    
    Optional<PostFavorite> findByPostIdAndUserId(Long postId, Long userId);
    
    boolean existsByPostIdAndUserId(Long postId, Long userId);
    
    void deleteByPostIdAndUserId(Long postId, Long userId);
    
    @Modifying
    @Transactional
    @Query("DELETE FROM PostFavorite pf WHERE pf.post.id = :postId")
    void deleteByPostId(@Param("postId") Long postId);
    
    long countByPostId(Long postId);
} 